package com.aaa.marketresearch.android.widget;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.aaa.marketresearch.android.app.FamilyMemberScreen;
import com.aaa.marketresearch.android.app.MarketScreen;
import com.aaa.marketresearch.android.app.R;
import com.aaa.marketresearch.android.data.UserInfo;
import com.aaa.marketresearch.android.data.domain.FamilyMember;
import com.aaa.marketresearch.android.data.domain.Market;
import com.aaa.marketresearch.android.util.SimpleLog;

import java.util.List;

/**
 * @author Nail
 */
public class FamilyMemberAdapter extends BaseAdapter {
    private Context context;
    LayoutInflater lInflater;
    List<FamilyMember> familyMembers;

    public FamilyMemberAdapter(Context context, List<FamilyMember> familyMembers) {
        this.context = context;
        this.familyMembers = familyMembers;
        this.lInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return familyMembers.size();
    }

    @Override
    public Object getItem(int position) {
        return this.familyMembers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // товар по позиции
    FamilyMember getFamilyMember(int position) {
        return ((FamilyMember) getItem(position));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // используем созданные, но не используемые view
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.family_member_view, parent, false);
        }

        final FamilyMember familyMember = this.getFamilyMember(position);

        TextView textView = (TextView) view.findViewById(R.id.familyMemberName);
        textView.setText(familyMember.getName());

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SimpleLog.info("choseFamilyMemberButton");

                UserInfo userInfo = UserInfo.getInstance();
                userInfo.setCurrentFamilyMember(familyMember);

                Intent intent = new Intent(FamilyMemberAdapter.this.context, MarketScreen.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                FamilyMemberAdapter.this.context.startActivity(intent);
            }
        });

        Button editButton = (Button) view.findViewById(R.id.editFamilyMember);
        editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SimpleLog.info("editFamilyMemberButton");

                UserInfo userInfo = UserInfo.getInstance();
                userInfo.setCurrentFamilyMember(familyMember);

                Intent intent = new Intent(FamilyMemberAdapter.this.context, FamilyMemberScreen.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                FamilyMemberAdapter.this.context.startActivity(intent);
            }
        });

        return view;
    }
}